{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Country Name</th>\n",
       "      <th>longitude</th>\n",
       "      <th>latitude</th>\n",
       "      <th>1960</th>\n",
       "      <th>1961</th>\n",
       "      <th>1962</th>\n",
       "      <th>1963</th>\n",
       "      <th>1964</th>\n",
       "      <th>1965</th>\n",
       "      <th>1966</th>\n",
       "      <th>...</th>\n",
       "      <th>2009</th>\n",
       "      <th>2010</th>\n",
       "      <th>2011</th>\n",
       "      <th>2012</th>\n",
       "      <th>2013</th>\n",
       "      <th>2014</th>\n",
       "      <th>2015</th>\n",
       "      <th>2016</th>\n",
       "      <th>2017</th>\n",
       "      <th>2018</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>United Kingdom</td>\n",
       "      <td>-0.07858</td>\n",
       "      <td>51.50476</td>\n",
       "      <td>7.232805e+10</td>\n",
       "      <td>7.669436e+10</td>\n",
       "      <td>8.060194e+10</td>\n",
       "      <td>8.544377e+10</td>\n",
       "      <td>9.338760e+10</td>\n",
       "      <td>1.010000e+11</td>\n",
       "      <td>1.070000e+11</td>\n",
       "      <td>...</td>\n",
       "      <td>2390000000000</td>\n",
       "      <td>2450000000000</td>\n",
       "      <td>2630000000000</td>\n",
       "      <td>2680000000000</td>\n",
       "      <td>2750000000000</td>\n",
       "      <td>3030000000000</td>\n",
       "      <td>2900000000000</td>\n",
       "      <td>2660000000000</td>\n",
       "      <td>2640000000000</td>\n",
       "      <td>2830000000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>United States</td>\n",
       "      <td>-77.04026</td>\n",
       "      <td>38.85169</td>\n",
       "      <td>5.430000e+11</td>\n",
       "      <td>5.630000e+11</td>\n",
       "      <td>6.050000e+11</td>\n",
       "      <td>6.390000e+11</td>\n",
       "      <td>6.860000e+11</td>\n",
       "      <td>7.440000e+11</td>\n",
       "      <td>8.150000e+11</td>\n",
       "      <td>...</td>\n",
       "      <td>14400000000000</td>\n",
       "      <td>15000000000000</td>\n",
       "      <td>15500000000000</td>\n",
       "      <td>16200000000000</td>\n",
       "      <td>16800000000000</td>\n",
       "      <td>17500000000000</td>\n",
       "      <td>18200000000000</td>\n",
       "      <td>18700000000000</td>\n",
       "      <td>19500000000000</td>\n",
       "      <td>20500000000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Russian Federation</td>\n",
       "      <td>37.59411</td>\n",
       "      <td>55.75306</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>1220000000000</td>\n",
       "      <td>1520000000000</td>\n",
       "      <td>2050000000000</td>\n",
       "      <td>2210000000000</td>\n",
       "      <td>2300000000000</td>\n",
       "      <td>2060000000000</td>\n",
       "      <td>1360000000000</td>\n",
       "      <td>1280000000000</td>\n",
       "      <td>1580000000000</td>\n",
       "      <td>1660000000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>France</td>\n",
       "      <td>2.29363</td>\n",
       "      <td>48.87372</td>\n",
       "      <td>6.265147e+10</td>\n",
       "      <td>6.834674e+10</td>\n",
       "      <td>7.631378e+10</td>\n",
       "      <td>8.555111e+10</td>\n",
       "      <td>9.490659e+10</td>\n",
       "      <td>1.020000e+11</td>\n",
       "      <td>1.110000e+11</td>\n",
       "      <td>...</td>\n",
       "      <td>2690000000000</td>\n",
       "      <td>2640000000000</td>\n",
       "      <td>2860000000000</td>\n",
       "      <td>2680000000000</td>\n",
       "      <td>2810000000000</td>\n",
       "      <td>2850000000000</td>\n",
       "      <td>2440000000000</td>\n",
       "      <td>2470000000000</td>\n",
       "      <td>2590000000000</td>\n",
       "      <td>2780000000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>China</td>\n",
       "      <td>116.39213</td>\n",
       "      <td>39.90071</td>\n",
       "      <td>5.971647e+10</td>\n",
       "      <td>5.005687e+10</td>\n",
       "      <td>4.720936e+10</td>\n",
       "      <td>5.070680e+10</td>\n",
       "      <td>5.970834e+10</td>\n",
       "      <td>7.043627e+10</td>\n",
       "      <td>7.672029e+10</td>\n",
       "      <td>...</td>\n",
       "      <td>5100000000000</td>\n",
       "      <td>6090000000000</td>\n",
       "      <td>7550000000000</td>\n",
       "      <td>8530000000000</td>\n",
       "      <td>9570000000000</td>\n",
       "      <td>10400000000000</td>\n",
       "      <td>11000000000000</td>\n",
       "      <td>11100000000000</td>\n",
       "      <td>12100000000000</td>\n",
       "      <td>13600000000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 62 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         Country Name  longitude  latitude          1960          1961  \\\n",
       "0      United Kingdom   -0.07858  51.50476  7.232805e+10  7.669436e+10   \n",
       "1       United States  -77.04026  38.85169  5.430000e+11  5.630000e+11   \n",
       "2  Russian Federation   37.59411  55.75306           NaN           NaN   \n",
       "3              France    2.29363  48.87372  6.265147e+10  6.834674e+10   \n",
       "4               China  116.39213  39.90071  5.971647e+10  5.005687e+10   \n",
       "\n",
       "           1962          1963          1964          1965          1966  ...  \\\n",
       "0  8.060194e+10  8.544377e+10  9.338760e+10  1.010000e+11  1.070000e+11  ...   \n",
       "1  6.050000e+11  6.390000e+11  6.860000e+11  7.440000e+11  8.150000e+11  ...   \n",
       "2           NaN           NaN           NaN           NaN           NaN  ...   \n",
       "3  7.631378e+10  8.555111e+10  9.490659e+10  1.020000e+11  1.110000e+11  ...   \n",
       "4  4.720936e+10  5.070680e+10  5.970834e+10  7.043627e+10  7.672029e+10  ...   \n",
       "\n",
       "             2009            2010            2011            2012  \\\n",
       "0   2390000000000   2450000000000   2630000000000   2680000000000   \n",
       "1  14400000000000  15000000000000  15500000000000  16200000000000   \n",
       "2   1220000000000   1520000000000   2050000000000   2210000000000   \n",
       "3   2690000000000   2640000000000   2860000000000   2680000000000   \n",
       "4   5100000000000   6090000000000   7550000000000   8530000000000   \n",
       "\n",
       "             2013            2014            2015            2016  \\\n",
       "0   2750000000000   3030000000000   2900000000000   2660000000000   \n",
       "1  16800000000000  17500000000000  18200000000000  18700000000000   \n",
       "2   2300000000000   2060000000000   1360000000000   1280000000000   \n",
       "3   2810000000000   2850000000000   2440000000000   2470000000000   \n",
       "4   9570000000000  10400000000000  11000000000000  11100000000000   \n",
       "\n",
       "             2017            2018  \n",
       "0   2640000000000   2830000000000  \n",
       "1  19500000000000  20500000000000  \n",
       "2   1580000000000   1660000000000  \n",
       "3   2590000000000   2780000000000  \n",
       "4  12100000000000  13600000000000  \n",
       "\n",
       "[5 rows x 62 columns]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "# 下载数据材料，并保存到本地路径，并对file变量就行对应的更改...\n",
    "file = \"GDP.csv\"\n",
    "df_GDP = pd.read_csv(file, header=0)\n",
    "\n",
    "# 查看数据 \n",
    "df_GDP.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Country Name</th>\n",
       "      <th>国家</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>United Kingdom</td>\n",
       "      <td>英国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>United States</td>\n",
       "      <td>美国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Russian Federation</td>\n",
       "      <td>俄罗斯</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>France</td>\n",
       "      <td>法国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>China</td>\n",
       "      <td>中国</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         Country Name   国家\n",
       "0      United Kingdom   英国\n",
       "1       United States   美国\n",
       "2  Russian Federation  俄罗斯\n",
       "3              France   法国\n",
       "4               China   中国"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "country_dict = {\n",
    "    \"United Kingdom\": \"英国\", \n",
    "    \"United States\": \"美国\", \n",
    "    \"Russian Federation\": \"俄罗斯\", \n",
    "    \"France\": \"法国\", \n",
    "    \"China\": \"中国\" \n",
    "}\n",
    "df_GDP[\"国家\"] = df_GDP[\"Country Name\"].map(country_dict)\n",
    "df_GDP[[\"Country Name\", \"国家\"]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     19.50万亿人民币\n",
       "1    141.28万亿人民币\n",
       "2     11.44万亿人民币\n",
       "3     19.16万亿人民币\n",
       "4     93.73万亿人民币\n",
       "Name: 2018, dtype: object"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 定义函数:将数字类型美元计价GDP的变量转换为以万亿人民币为单位计价的数据\n",
    "def dollar_to_rmb(x):\n",
    "    if np.isnan(x):\n",
    "        return np.nan\n",
    "    else:\n",
    "        _value = x / 1000000000000 * 6.8918\n",
    "        return \"%.2f万亿人民币\"%(_value)\n",
    "# 利用apply，将函数dollar_to_rmb的功能，作用到\"2018\"列，进行列方向的计算\n",
    "df_GDP[\"2018\"].apply(dollar_to_rmb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    0.50万亿人民币\n",
       "1    3.74万亿人民币\n",
       "2          NaN\n",
       "3    0.43万亿人民币\n",
       "4    0.41万亿人民币\n",
       "Name: 1960, dtype: object"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_GDP[\"1960\"].apply(dollar_to_rmb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     19.50万亿人民币\n",
       "1    141.28万亿人民币\n",
       "2     11.44万亿人民币\n",
       "3     19.16万亿人民币\n",
       "4     93.73万亿人民币\n",
       "Name: 2018, dtype: object"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_GDP[\"2018\"].apply(\n",
    "    lambda x: x if np.isnan(x) else \"%.2f万亿人民币\"%(x / 1000000000000 * 6.8918))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     7.20%\n",
       "1     5.13%\n",
       "2     5.06%\n",
       "3     7.34%\n",
       "4    12.40%\n",
       "dtype: object"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 定义了一个计算GDP增长率的匿名函数\n",
    "df_GDP[[\"2017\", \"2018\"]].apply(\n",
    "    lambda x: \"%.2f\"%((x[\"2018\"]-x[\"2017\"])/x[\"2017\"]*100) + \"%\", axis=1)"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
  },
  "kernelspec": {
   "display_name": "Python 3.9.1 64-bit",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.1"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
